﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ras2ClassLib;

namespace Ras2Form
{
    public partial class SongsView : UserControl
    {
        public SongsView()
        {
            InitializeComponent();
        }

        private void OnLoad(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                SongController controller = new SongController();
                foreach (UISong s in controller.GetAllSongs())
                {
                    m_lvSongs.Items.Add(CreateListViewItem(s));
                }
            }
        }

        private ListViewItem CreateListViewItem(UISong row)
        {
            ListViewItem item = new ListViewItem(row.Name);

            item.SubItems.Add(row.PerformerName);

            item.ImageIndex = 0;
            item.Tag = row;

            return item;
        }

        public void OnNewSong()
        {
            using (SongDlg dlg = new SongDlg())
            {
                dlg.NewSong = true;
                dlg.Song = new UISong();
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    UISong song = dlg.Song;
                    SongController controller = new SongController();

                    controller.AddSong(song);
                    m_lvSongs.Items.Add(CreateListViewItem(song));
                }
            }
        }

        public void OnEditSong()
        {
            if (m_lvSongs.SelectedItems.Count == 1)
            {
                ListViewItem listViewItem = m_lvSongs.SelectedItems[0];

                UISong song = (UISong)listViewItem.Tag;
                using (SongDlg dlg = new SongDlg())
                {
                    dlg.Song = song;
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        song = dlg.Song;
                        SongController controller = new SongController();

                        controller.UpdateSong(song);
                        int nIndex = listViewItem.Index;

                        m_lvSongs.Items.Remove(listViewItem);
                        m_lvSongs.Items.Insert(nIndex, CreateListViewItem(song));
                    }
                }
            }
        }

        public void OnDeleteSong()
        {
            if (m_lvSongs.SelectedItems.Count == 1)
            {
                if (MessageBox.Show("Ertu viss um að þú viljir eyða út þess lagi?", "Eyða lagi", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    ListViewItem listViewItem = m_lvSongs.SelectedItems[0];

                    UISong song = (UISong)listViewItem.Tag;
                    SongController controller = new SongController();

                    controller.DeleteSong(song);
                    m_lvSongs.Items.Remove(listViewItem);
                }
            }
        }

        private void OnDoubleClick(object sender, MouseEventArgs e)
        {
            OnEditSong();
        }
    }
}
